package org.link.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.link.bean.User;
import org.link.util.DBUtil;

/**
 * 
 * project_name:sqlserverdemo
 * package_name:org.link.dao
 * file_name:UserDao.java
 * @author Link
 *
 */
public class UserDao {

	public User selectById(int id){
		Connection conn = DBUtil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		User user = null;
		try {
			pstmt = conn.prepareStatement("select * from t_user where id = ?");
			pstmt.setInt(1, id);
			rs = pstmt.executeQuery();
			while(rs.next()){
				user = new User();
				user.setId(rs.getInt("id"));
				user.setHeight(rs.getFloat("height"));
				user.setBirth(rs.getDate("birth"));
				user.setUsername(rs.getString("username"));
				user.setUserpass(rs.getString("userpass"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeConnection(pstmt, conn, rs);
		}
		return user;
	}
	
	public static void main(String[] args) {
		System.out.println(new UserDao().selectById(1));
	}
	
}




